import os
from loguru import logger

def save_task_data_func(agent, filename: str, data: str) -> str:
    if not agent.current_project or not agent.current_task:
        logger.error("Current project and task not set. Cannot save data.")
        return "Error: Current project and task not set. Cannot save data."
    
    task_dir = os.path.join("projects", agent.current_project, agent.current_task)
    file_path = os.path.join(task_dir, filename)
    try:
        os.makedirs(task_dir, exist_ok=True)
        with open(file_path, 'w', encoding='utf-8') as f:
            f.write(data)
        logger.info(f"Successfully saved data to {file_path}")
        return f"Successfully saved data to {file_path}"
    except Exception as e:
        logger.error(f"Error saving data to {file_path}: {e}", exc_info=True)
        return f"Error saving data to {file_path}: {e}"